package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsXzz1Bo;
import com.ruoyi.tb.lmt.domain.vo.McgsXzz1Vo;
import com.ruoyi.tb.lmt.domain.McgsXzz1;
import com.ruoyi.tb.lmt.mapper.McgsXzz1Mapper;
import com.ruoyi.tb.lmt.service.IMcgsXzz1Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_小闸组1_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsXzz1ServiceImpl implements IMcgsXzz1Service {

    private final McgsXzz1Mapper baseMapper;

    /**
     * 查询制动阀试验台_小闸组1_MCGS
     */
    @Override
    public McgsXzz1Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_小闸组1_MCGS列表
     */
    @Override
    public TableDataInfo<McgsXzz1Vo> queryPageList(McgsXzz1Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsXzz1> lqw = buildQueryWrapper(bo);
        // 在这里添加根据 createTime 降序排序
        pageQuery.setOrderByColumn("createTime");
        pageQuery.setIsAsc("desc");
        Page<McgsXzz1Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_小闸组1_MCGS列表
     */
    @Override
    public List<McgsXzz1Vo> queryList(McgsXzz1Bo bo) {
        LambdaQueryWrapper<McgsXzz1> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsXzz1> buildQueryWrapper(McgsXzz1Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsXzz1> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsXzz1::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsXzz1::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsXzz1::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsXzz1::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsXzz1::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsXzz1::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsFh()), McgsXzz1::getMcgsFh, bo.getMcgsFh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsXzz1::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz1()), McgsXzz1::getMcgsXz1, bo.getMcgsXz1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz2()), McgsXzz1::getMcgsXz2, bo.getMcgsXz2());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz3()), McgsXzz1::getMcgsXz3, bo.getMcgsXz3());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz4()), McgsXzz1::getMcgsXz4, bo.getMcgsXz4());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz5()), McgsXzz1::getMcgsXz5, bo.getMcgsXz5());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz6()), McgsXzz1::getMcgsXz6, bo.getMcgsXz6());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz7()), McgsXzz1::getMcgsXz7, bo.getMcgsXz7());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz8()), McgsXzz1::getMcgsXz8, bo.getMcgsXz8());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz9()), McgsXzz1::getMcgsXz9, bo.getMcgsXz9());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz10()), McgsXzz1::getMcgsXz10, bo.getMcgsXz10());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz11()), McgsXzz1::getMcgsXz11, bo.getMcgsXz11());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz12()), McgsXzz1::getMcgsXz12, bo.getMcgsXz12());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz13()), McgsXzz1::getMcgsXz13, bo.getMcgsXz13());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz14()), McgsXzz1::getMcgsXz14, bo.getMcgsXz14());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz15()), McgsXzz1::getMcgsXz15, bo.getMcgsXz15());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz16()), McgsXzz1::getMcgsXz16, bo.getMcgsXz16());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz17()), McgsXzz1::getMcgsXz17, bo.getMcgsXz17());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz18()), McgsXzz1::getMcgsXz18, bo.getMcgsXz18());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXz19()), McgsXzz1::getMcgsXz19, bo.getMcgsXz19());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsXljc()), McgsXzz1::getMcgsXljc, bo.getMcgsXljc());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsXzz1::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsXzz1::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsXzz1::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsXzz1::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsXzz1::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsXzz1::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsXzz1::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_小闸组1_MCGS
     */
    @Override
    public Boolean insertByBo(McgsXzz1Bo bo) {
        McgsXzz1 add = BeanUtil.toBean(bo, McgsXzz1.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_小闸组1_MCGS
     */
    @Override
    public Boolean updateByBo(McgsXzz1Bo bo) {
        McgsXzz1 update = BeanUtil.toBean(bo, McgsXzz1.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsXzz1 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_小闸组1_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }

    @Override
    public List<McgsXzz1> selectListByDate(LocalDate date) {
        return baseMapper.selectListByDate(date);
    }
}
